'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:TaskNewtaskCtrl
 * @description
 * # TaskNewtaskCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('TaskNewtaskCtrl', function($scope,openApi, DataService, $location, Util, $rootScope, $filter, TaskStaticData) {
        var vm = this;
        this.param = {};
        this.saveInfo = function() {
            var params = this.param;
            // $routeParams.id通过id确定唯一回显id
            Util.savaParamForReview(params, "TaskNewtaskCtrl");
        };
        this.goaddjoinpeople = function(type) {
            this.saveInfo();
            $location.path("task/addjoinpeople/" + type)
        };
        this.showSheet = function(type) {
            var sheetObj = this.sheetObj[type]
            this.sheetObj.type = type
            this.sheetObj.tabList = sheetObj.tabList
            this.sheetObj.sheettitle = sheetObj.sheettitle
            this.sheetObj.ifshowSheet = true
            $rootScope.showMasker = true
        };
        //------------------------------多选框
        // 底部各种类型选框添加
        this.sheetObj = {};
        // ---------------------------------
        var statusObj = {};
        statusObj.sheettitle = "创建任务至";
        statusObj.tabList = TaskStaticData.statusList;
        statusObj.callback = function(item) {
            vm.param.status = item.selectTab
            vm.param.statusContent = item.selectTabContent
        };
        // ---------------------------------
        var priorityObj = {};
        priorityObj.sheettitle = "优先级";
        priorityObj.tabList = TaskStaticData.priorityList;
        priorityObj.callback = function(item) {
            vm.param.priority = item.selectTab
            vm.param.priorityContent = item.selectTabContent
        };
        // -----------------------------------
        var repetitionObj = {};
        repetitionObj.sheettitle = "";
        repetitionObj.tabList = TaskStaticData.repetitionList;
        repetitionObj.callback = function(item) {
            vm.param.repetition = item.selectTab
            vm.param.repetitionContent = item.selectTabContent
        };
        // ---------------------------------
        var startNoticeTimeObj = {};
        startNoticeTimeObj.sheettitle = "任务开始提醒";
        startNoticeTimeObj.tabList = TaskStaticData.remindList;
        startNoticeTimeObj.callback = function(item) {
            vm.param.startNoticeTime = item.selectTab
            vm.param.startNoticeTimeContent = item.selectTabContent
        };
        var endNoticeTimeObj = {};
        endNoticeTimeObj.sheettitle = "任务结束提醒";
        endNoticeTimeObj.tabList = TaskStaticData.remindList;
        endNoticeTimeObj.callback = function(item) {
            vm.param.endNoticeTime = item.selectTab
            vm.param.endNoticeTimeContent = item.selectTabContent
        };
        // -----------------------------------
        this.sheetObj.statusObj = statusObj;
        this.sheetObj.priorityObj = priorityObj;
        this.sheetObj.repetitionObj = repetitionObj;
        this.sheetObj.startNoticeTimeObj = startNoticeTimeObj;
        this.sheetObj.endNoticeTimeObj = endNoticeTimeObj;
        this.submit = function() {
            var param;
            param = angular.copy(this.param);
            console.log(param);
            if (param.subTasks && param.subTasks.length > 0) {
                param.subTaskStr = JSON.stringify(param.subTasks)
            }
            if (param.joinPersons) {
                var userIds = param.joinPersons.map(function(item) {
                    return {
                        userId: item.userId
                    }
                })
                param.joinPersionStr = JSON.stringify(userIds);
            }
            delete param.subTasks;
            delete param.joinPersons;
            var ckeched = this.checksubmit();
            if (!ckeched) {
                return
            }
            DataService.getData(openApi.addTask, param).then(function(data, success) {
                Util.Toast(data, {
                    callback: function() {
                        Util.clearParamForReview()
                        Util.goBack();
                    }
                })
            }.bind(this))

        };
        this.checksubmit = function() {
            var globalcheck = true;
            if (!this.param.title) {
                Util.Toast("请填写任务名称")
                globalcheck = false;
                return globalcheck
            }
            if (!this.param.endDate) {
                Util.Toast("请选择结束日期")
                globalcheck = false;
                return globalcheck
            }
            return globalcheck

        };
        vm.deljoinconfirm = function(index) {
            vm.curdeljoinindex = index;
            navigator.notification.confirm(
                '确定删除吗？',
                vm.deljoinper,
                '提示',
                ['确定', '取消']
            );
        };
        vm.deltaskconfirm = function(index) {
            vm.curdeltaskindex = index;
            navigator.notification.confirm(
                '确定删除吗？',
                vm.deltask,
                '提示',
                ['确定', '取消']
            );
        };
        vm.deltask = function(button) {
            var checked;
            if (button == 1) {
                vm.param.subTasks.splice(vm.curdeljoinindex, 1);
            }
        };
        vm.deljoinper = function(button) {
            var checked;
            if (button == 1) {
                checked = vm.checkifinsubtask();
                if (checked) {
                    Util.Toast("该参与人有负责的子任务")
                    return;
                }
                vm.param.joinPersons.splice(vm.curdeljoinindex, 1);
                $scope.$apply();
            }
        };
        vm.checkifinsubtask = function() {
            var checked = false;
            var user = {};
            user = vm.param.joinPersons[vm.curdeljoinindex];
            if (vm.param.subTasks && vm.param.subTasks.length > 0) {
                vm.param.subTasks.forEach(function(item, index) {
                    if (item.userId == user.userId) {
                        checked = true;
                        return;
                    }
                });
            }
            return checked;
        };
        this.addsubtask = function() {
            this.saveInfo();
            $location.path("task/subtask/newtask")
        };
        this.togglePublic = function() {
            this.param.visibilityInPublic = this.param.visibilityInPublic ? 0 : 1;
            this.param.visibilityInPublicContent = TaskStaticData.getContentBykey(this.param.visibilityInPublic, "visibilityInPublic");;
        };
        this.getUserinfo = function() {
            DataService.getData(openApi.userInfo).then(function(data) {
                // 如果没有用户id默认为当前用户
                if (!this.param.userId) {
                    this.param.userId = data.userId
                    this.param.userName = data.name
                }
            }.bind(this))
        };
        this.setDeault = function() {
            // 设置任务状态
            this.param.status = this.param.status || TaskStaticData.statusList[0].value;
            this.param.statusContent = TaskStaticData.getContentBykey(this.param.status, "status");
            // 设置开始时间
            this.param.startDate = this.param.startDate || $filter("dateFmt")(new Date(),"yyyy-MM-dd hh:mm:ss")
                // 设置优先级
            this.param.priority = this.param.priority || TaskStaticData.priorityList[0].value;
            this.param.priorityContent = TaskStaticData.getContentBykey(this.param.priority, "priority");
            // 设置重复
            this.param.repetition = this.param.repetition || TaskStaticData.repetitionList[0].value;
            this.param.repetitionContent = TaskStaticData.getContentBykey(this.param.repetition, "repetition");
            // 设置开始提醒
            this.param.startNoticeTime = this.param.startNoticeTime || TaskStaticData.remindList[1].value;
            this.param.startNoticeTimeContent = TaskStaticData.getContentBykey(this.param.startNoticeTime, "remind");
            // 设置完成提醒
            this.param.endNoticeTime = this.param.endNoticeTime || TaskStaticData.remindList[1].value;
            this.param.endNoticeTimeContent = TaskStaticData.getContentBykey(this.param.endNoticeTime, "remind");
            // 设置权限
            this.param.visibilityInPublic = this.param.visibilityInPublic || TaskStaticData.visibilityInPublicList[0].value;
            this.param.visibilityInPublicContent = TaskStaticData.getContentBykey(this.param.visibilityInPublic, "visibilityInPublic");
        };
        this.init = function() {
            this.param = Util.getParamForReview("TaskNewtaskCtrl")
            this.getUserinfo();
            this.setDeault();
        };
        this.init();

    });
